Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports DotNetNuke.Common.Globals
Imports DotNetNuke.Services.Exceptions.Exceptions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs
Imports DotNetNuke.Security

Namespace Avastone.DNN.Modules.HouseIndex

    Public Class HouseIndex
        Inherits PortalModuleBase

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents litIndex As System.Web.UI.WebControls.Literal

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private ShowHidden As Boolean
        Private ShowAdmin As Boolean
        Private DescriptionsVisible As Boolean
        Private KeywordsVisible As Boolean
        Private ModulesVisible As Boolean
        Private SortByTabName As Boolean
        Private CssClassPrefix As String

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    Me.LoadSettings()
                    Me.BindIndex()
                End If
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub

#Region " Private Procedures "

        Private Sub LoadSettings()

            Try
                If Not Settings("ShowHidden") Is Nothing Then
                    Me.ShowHidden = Boolean.Parse(Settings("ShowHidden").ToString)
                Else
                    Me.ShowHidden = False
                End If

                If Not Settings("ShowAdmin") Is Nothing Then
                    Me.ShowAdmin = Boolean.Parse(Settings("ShowAdmin").ToString)
                Else
                    Me.ShowAdmin = False
                End If

                If Not Settings("DescriptionsVisible") Is Nothing Then
                    Me.DescriptionsVisible = Boolean.Parse(Settings("DescriptionsVisible").ToString)
                Else
                    Me.DescriptionsVisible = True
                End If

                If Not Settings("KeywordsVisible") Is Nothing Then
                    Me.KeywordsVisible = Boolean.Parse(Settings("KeywordsVisible").ToString)
                Else
                    Me.KeywordsVisible = True
                End If

                If Not Settings("ModulesVisible") Is Nothing Then
                    Me.ModulesVisible = Boolean.Parse(Settings("ModulesVisible").ToString)
                Else
                    Me.ModulesVisible = True
                End If

                If Not Settings("SortByTabName") Is Nothing Then
                    Me.SortByTabName = Boolean.Parse(Settings("SortByTabName").ToString)
                Else
                    Me.SortByTabName = False
                End If

                If Not Settings("CssClassPrefix") Is Nothing Then
                    Me.CssClassPrefix = CType(Settings("CssClassPrefix"), String)
                Else
                    Me.CssClassPrefix = ""
                End If

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Sub

        Private Sub BindIndex()

            Try
                Dim htmMenu As New Text.StringBuilder
                Dim objTabController As TabController = New TabController
                Dim objPortalTabs As ArrayList  ' TabItem objects

                If Me.ShowAdmin Then
                    If Not PortalSettings.DesktopTabs Is Nothing Then
                        htmMenu.Append("<ul class=""" & Me.CssClassPrefix & "PageList"">")
                        For Each objTab As TabInfo In PortalSettings.DesktopTabs
                            If objTab.ParentId < 1 And Not objtab.IsDeleted Then
                                htmMenu.Append(Me.RenderTab(objTab.TabId))
                            End If
                        Next
                        htmMenu.Append("</ul>")
                    End If
                Else
                    objPortalTabs = GetPortalTabs(PortalSettings.DesktopTabs, False, Me.ShowHidden, False, True)
                    If Not objPortalTabs Is Nothing Then
                        htmMenu.Append("<ul class=""" & Me.CssClassPrefix & "PageList"">")
                        For Each objTab As TabInfo In objPortalTabs
                            If objTab.ParentId < 1 And Not objtab.IsDeleted Then
                                htmMenu.Append(Me.RenderTab(objTab.TabId))
                            End If
                        Next
                    End If
                    htmMenu.Append("</ul>")
                End If

                Me.litIndex.Text = htmMenu.ToString

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Sub

        Private Function RenderTabList(ByVal parentId As Integer) As String

            Try
                Dim htmTabList As New Text.StringBuilder
                Dim objTabController As TabController = New TabController
                Dim objTabs As ArrayList  ' TabInfo objects
                Dim parentLevel As Integer

                ' Get passed parent tab
                Dim objParent As New TabInfo
                objParent = objTabController.GetTab(parentId)

                ' Set the parent level and get tabs
                parentLevel = objParent.Level
                objTabs = objTabController.GetTabsByParentId(parentId)

                htmTabList.Append("<ul>" & vbCrLf)

                ' Render tabs
                If Not objTabs Is Nothing Then
                    For Each objTab As TabInfo In objTabs
                        If objTab.IsDeleted = False And (objTab.IsVisible = True Or Me.ShowHidden = True) Then
                            If objTab.Level = parentLevel + 1 Then
                                htmTabList.Append(Me.RenderTab(objTab.TabID))
                            End If
                        End If
                    Next
                End If

                htmTabList.Append("</ul>" & vbCrLf)

                Return htmTabList.ToString

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Function

        Private Function RenderTab(ByVal itemId As Integer) As String

            Try
                Dim htmTab As New Text.StringBuilder
                Dim objTabController As TabController = New TabController
                Dim objTab As New TabInfo

                objTab = objTabController.GetTab(itemId)

                If PortalSecurity.IsInRoles(objTab.AuthorizedRoles) Then
                    htmTab.Append("<li class=""" & Me.CssClassPrefix & "PageItem"">")
                    Dim htmTabName As String = "<span class=""" & Me.CssClassPrefix & "PageName"">" & objTab.TabName & "</span>"

                    If objTab.DisableLink Then
                        htmTab.Append(htmTabName)
                    Else
                        'htmTab.Append("<a href=""" & Page.ResolveUrl("~/default.aspx?tabid=" & objTab.TabID) & """>" & htmTabName & "</a>")
                        If objTab.FullUrl.IndexOf("javascript:") = -1 Then
                            htmTab.Append("<a href=""" & objTab.FullUrl & """>" & htmTabName & "</a>")
                        Else
                            htmTab.Append("<a href=""#"" onclick=""" & objTab.FullUrl & """>" & htmTabName & "</a>")
                        End If
                    End If
                    If Me.DescriptionsVisible And objTab.Description.Length > 0 Then
                        htmTab.Append(": <span class=""" & Me.CssClassPrefix & "PageDescription"">" & objTab.Description & "</span>")
                    End If
                    If Me.KeywordsVisible And objTab.KeyWords.Length > 0 Then
                        htmTab.Append(" (<span class=""" & Me.CssClassPrefix & "PageKeywords"">" & objTab.KeyWords & "</span>)")
                    End If
                    If Me.ModulesVisible Then
                        htmTab.Append(Me.RenderModuleList(objTab.TabID))
                    End If
                    If objTabController.GetTabsByParentId(objTab.TabID).Count > 0 Then
                        htmTab.Append(vbCrLf & Me.RenderTabList(objTab.TabID))
                    End If
                    htmTab.Append("</li>" & vbCrLf)
                Else
                    Return ""
                End If

                Return htmTab.ToString

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Function

        Private Function RenderModuleList(ByVal tabId As Integer) As String
            Try
                Dim htmModuleList As New Text.StringBuilder
                Dim objModuleController As New ModuleController
                Dim list As ArrayList = objModuleController.GetPortalTabModules(PortalId, tabId)

                If Not list Is Nothing And list.Count > 0 Then
                    'htmModuleList.Append("<blockquote class=" & Me.CssClassPrefix & "NormalSmall" & ">" & vbCrLf)
                    htmModuleList.Append("<br/>" & vbCrLf)
                    htmModuleList.Append(Chr(133) & " ")
                    For Each objModule As ModuleInfo In list
                        'htmModuleList.Append("<li class=" & Me.CssClassPrefix & "NormalSmall" & ">")
                        htmModuleList.Append(objModule.ModuleTitle & ", ")
                        'htmModuleList.Append("</li>" & vbCrLf)
                    Next
                    'htmModuleList.Append("</blockquote>" & vbCrLf)
                End If

                Return "<span class=""" & Me.CssClassPrefix & "PageModules"">" & htmModuleList.ToString.Trim(", ".ToCharArray) & "</span>"

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Function

#End Region

#Region " Public Procedures "
        Public Function GetCssClass() As String
            Return Me.CssClassPrefix & "PageList"
        End Function

#End Region

    End Class

End Namespace
